package com.xmut.mall.member.controller;

import com.xmut.mall.member.annotation.CheckJwt;
import com.xmut.mall.member.entity.MemberCartEntity;
import com.xmut.mall.member.service.MemberCartService;
import com.xmut.mall.member.utils.RespBean;
import com.xmut.mall.member.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("member/membercart")
public class MemberCartController {

    @Autowired
    private MemberCartService memberCartService;

    /**
     * 获取用户购物车中进行结算的购物车项
     * @param userId
     * @return
     */
    @CheckJwt
    @GetMapping("/getUserCheckedCartEntityItem/{userId}")
    public RespBean getUserCheckedCartEntityItem(@PathVariable Long userId){
        List<MemberCartEntity> memberCartEntities = memberCartService.getUserCheckedCartEntityItem(userId);
        return RespBean.success("获取成功",memberCartEntities);
    }

    /**
     * 获取用户购物车中目前选中的购物车项的信息
     * @param params
     * @return
     */
    @CheckJwt
    @PostMapping("/getUserCheckedCartItem")
    public RespBean getUserCheckedCartItem(@RequestBody Map<String,Object> params){
        List<CartVo> list = memberCartService.getUserCheckedCartItem(params);

        return RespBean.success("获取成功",list);
    }

    /**
     * 获取商品结算详情(购物车方式进行结算)
     * @param params
     * @return
     */
    @CheckJwt
    @PostMapping("/getGoodPayDetailTypeCART")
    public RespBean getGoodPayDetailTypeCART(@RequestBody Map<String,Object> params){
        return memberCartService.getGoodPayDetailTypeCART(params);
    }

    /**
     * 获取当前购物车的结算信息
     * @param memberId
     * @return
     */
    @CheckJwt
    @GetMapping("/getCartPriceDetail/{memberId}")
    public RespBean getCartPriceDetail(@PathVariable Long memberId){
        return memberCartService.getCartPriceDetail(memberId);
    }

    /**
     * 清空购物车
     * @param memberId
     * @return
     */
    @CheckJwt
    @DeleteMapping("/clearCart/{memberId}")
    public RespBean clearCart(@PathVariable Long memberId){
        return memberCartService.clearCart(memberId);
    }

    /**
     * 根据map删除购物车之中的项
     * @param params
     * @return
     */
    @CheckJwt
    @DeleteMapping("/deleteCartItemByMap")
    public RespBean deleteCartItemByMap(@RequestBody Map<String,Object> params){
        return memberCartService.deleteCartItemByMap(params);
    }

    /**
     * 改变购物车项选中的数量
     * @param params
     * @return
     */
    @CheckJwt
    @PutMapping("/changeCartItemNum")
    public RespBean changeCartItemNum(@RequestBody Map<String,Object> params){
        return memberCartService.changeCartItemNum(params);
    }

    /**
     * 购物车操作 选中或取消单个商品
     * @param params
     * @return
     */
    @CheckJwt
    @PutMapping("/checkSingleItem")
    public RespBean checkSingleItem(@RequestBody Map<String,Object> params){
        return memberCartService.checkSingleItem(params);
    }

    /**
     * 购物车操作全选 根据商铺
     * @return
     */
    @CheckJwt
    @PutMapping("/checkAllByStore")
    public RespBean checkAllByStore(@RequestBody Map<String,Object> params){
        return memberCartService.checkAllByStore(params);
    }

    /**
     * 购物车操作 全选
     * @return
     */
    @CheckJwt
    @GetMapping("/checkAll/{memberId}/{check}")
    public RespBean checkAll(@PathVariable("memberId") Long memberId,@PathVariable("check") Long check){
        return memberCartService.checkAll(memberId,check);
    }

    /**
     * 购物车详情页
     * @param userInfo
     * @return
     */
    @CheckJwt
    @GetMapping("/goCart/{userInfo}")
    public RespBean goCart(@PathVariable Long userInfo){
        List<CartVo> list = memberCartService.goCart(userInfo);
        return RespBean.success("获取购物车页面数据成功",list);
    }

    /**
     * 删除购物车的商品
     * @param memberId
     * @param skuId
     * @return
     */
    @CheckJwt
    @DeleteMapping("/deleteCartItem/{memberId}/{skuId}")
    public RespBean deleteCartItem(@PathVariable("memberId") Long memberId, @PathVariable("skuId") Long skuId) {
        return memberCartService.deleteCartItem(memberId,skuId);
    }

    /**
     * 获取购物车数量
     *
     * @param memberId
     * @return
     */
    @CheckJwt
    @GetMapping("/cartCount/{memberId}")
    public RespBean cartCount(@PathVariable Long memberId) {
        Integer count = memberCartService.cartCount(memberId);
        return RespBean.success("", count);
    }

    /**
     * 添加购物车
     *
     * @param entity
     * @return
     */
    @CheckJwt
    @PostMapping("/addCart")
    public RespBean addCart(@RequestBody MemberCartEntity entity) {
        return memberCartService.addCart(entity);
    }

    /**
     * 根据用户Id获取该用户的购物车列表
     *
     * @param memberId
     * @return
     */
    @CheckJwt
    @GetMapping("/getCart/{memberId}")
    public RespBean getCart(@PathVariable Long memberId) {
        List<MemberCartEntity> list = memberCartService.getCart(memberId);
        return RespBean.success("获取购物车信息成功", list);
    }
}
